#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QStateMachine>
#include <QAbstractTransition>
#include <QEventTransition>

#include <functional>
typedef std::function<void()> Function;

#include "selectstockw.h"
#include "monitorpanel.h"

QPushButton *CreateNavBtn(QString title, QString img, Function&& fn = nullptr)
{
    struct TItem
    {
        QPushButton *btn;
        QIcon ico1;
        QIcon ico2;
    };

    static QList <TItem> btns;
    QPushButton *btn = new QPushButton(title);

    btn->setStyleSheet(QString(
                       "QPushButton{border-style:none; color: #444444; font-size: 14px;}"
                       "QPushButton:checked{color: #ffa924;}"
                       "QPushButton:pressed{color: #ffa924;}"));

    btn->setCursor(Qt::PointingHandCursor);
    btn->setCheckable(true);

    QPixmap pixImg(img);
    QIcon ico1(pixImg.copy(0, 0, 20, 20));
    QIcon ico2(pixImg.copy(20, 0, 20, 20));
    btn->setIcon(ico1);
    btn->setIconSize(QSize(20, 20));
    btn->setFixedHeight(32);
    btns.append({btn, ico1, ico2});


    QObject::connect(btn, &QPushButton::clicked, [=]() {
        foreach (TItem item, btns) {
            if (item.btn != btn)
            {
                item.btn->setIcon(item.ico1);
                item.btn->setChecked(false);
            }
        }
        btn->setIcon(ico2);
        btn->setChecked(true);

        if(fn) fn();
    });


    QStateMachine *shadowStateMachine = new QStateMachine(btn);
    QState *normalState      = new QState;
    QState *hoverState       = new QState;
    QState *pressedState     = new QState;

    shadowStateMachine->addState(normalState);
    shadowStateMachine->addState(pressedState);

    normalState->assignProperty(btn, "icon", ico1);//normalState进入时，将btn的 icon属性设置为icon1
    hoverState->assignProperty(btn, "icon", ico2);
    pressedState->assignProperty(btn, "icon", ico2);

    QAbstractTransition *transition;//类的主要作用是定义和管理状态机中的状态过渡行为

    transition = new QEventTransition(btn, QEvent::MouseButtonPress);
    transition->setTargetState(pressedState);//设置转换的目标状态
    normalState->addTransition(transition);//用于将转换添加到特定状态中。当转换的条件满足时，状态机将自动从当前状态转移到目标状态

    transition = new QEventTransition(btn, QEvent::MouseButtonDblClick);//指定要监听的对象和事件类型
    transition->setTargetState(pressedState);
    normalState->addTransition(transition);

    transition = new QEventTransition(btn, QEvent::MouseButtonRelease);
    transition->setTargetState(pressedState);
    pressedState->addTransition(transition);

    shadowStateMachine->setInitialState(normalState);
    shadowStateMachine->start();


    return btn;
}


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{


    initUI();
    setAttribute(Qt::WA_StyledBackground);






}

MainWindow::~MainWindow()
{


}

void MainWindow::initUI()
{
    setMinimumSize(800,600);
    QWidget *w = new QWidget(this);

    m_pLeftPane = new QWidget(w);
    m_RightPane = new QWidget(w);
    m_pStackedWidget = new QStackedWidget(w);

    initLeftPane();
    initRightPane();

    QHBoxLayout *mainLayout = new QHBoxLayout(w);
    mainLayout->setContentsMargins(0, 10, 0, 10);
    mainLayout->setSpacing(0);
    mainLayout->addWidget(m_pLeftPane);
    mainLayout->addWidget(m_RightPane);

    setCentralWidget(w);
}

void MainWindow::initLeftPane()
{
    m_pLeftPane->setFixedWidth(120);
    QVBoxLayout *leftLayout = new QVBoxLayout(m_pLeftPane);
    leftLayout->setContentsMargins(0, 4, 0, 4);
    leftLayout->setSpacing(8);
    QPushButton *btn = CreateNavBtn("选股器", ":/img/virus.png", [=]() { m_pStackedWidget->setCurrentIndex(0); });
    btn->setChecked(true);
    leftLayout->addWidget(btn);
    leftLayout->addWidget( CreateNavBtn("选股器", ":/img/virus.png", [=]() { m_pStackedWidget->setCurrentIndex(1);}));
    leftLayout->addStretch(1);

    m_pLeftPane->setStyleSheet(".QWidget{border-right: 2px solid #f0f0f0;background-color: #282F3B;}");
}

void MainWindow::initRightPane()
{
    QVBoxLayout *rightLayout = new QVBoxLayout(m_RightPane);
    rightLayout->setContentsMargins(10, 4, 10, 4);
    rightLayout->setSpacing(8);

    SelectStockW *w0 = new SelectStockW(this);
    m_pStackedWidget->addWidget(w0);

    MonitorPanel *w1 = new MonitorPanel(this);
    m_pStackedWidget->addWidget(w1);

    rightLayout->addWidget(m_pStackedWidget);




}


